package com.hutao.sso.controller.authority;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.management.relation.RoleStatus;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hutao.sso.pojo.authority.RoleAuthority;
import com.hutao.sso.pojo.newAuthority.RoleAuthoritys;
import com.hutao.sso.pojo.newAuthority.Roles;
import com.hutao.sso.pojo.user.Users;
import com.hutao.sso.service.authority.RoleAuthorityService;
import com.hutao.sso.service.authority.RoleService;
import com.hutao.sso.service.authority.UserRolesService;
import com.hutao.sso.util.Page;
import com.hutao.sso.util.Result;
import com.hutao.sso.util.StringUtil;

@RequestMapping("/role")
@Controller
public class RoleController {
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private UserRolesService userRolesService;
	
	@Autowired
	private RoleAuthorityService roleAuthorityService;
	
	@GetMapping("/findRolesListByUserHospitalId")
	@ResponseBody
	public Map<String, Object> findRolesListByUserHospitalId(String id, HttpSession session) {
		Map<String, Object> params = new HashMap<>();
		Map<String, Object> results = new HashMap<>();
		Users user = (Users)session.getAttribute("current_user");
		params.put("medicalMainstayId", user!=null?user.getMedicalMainstayId():null);
		params.put("userId", id);
		
		List<Map<String, Object>> roleList = roleService.findRolesListByUserHospitalId(params);
		results.put("roleList", roleList);
		
		return results;
	}
	
	@PostMapping("/findRolesListPage")
	@ResponseBody
	public Map<String, Object> findRolesListPage(@RequestParam(value = "page", defaultValue = "1" ) int currentPage,
		String roleName, HttpSession session) {
		Map<String, Object> params = new HashMap<>();
		Map<String, Object> results = new HashMap<>();
		Users user = (Users)session.getAttribute("current_user");
		params.put("medicalMainstayId", user!=null?user.getMedicalMainstayId():null);
		params.put("roleName", roleName);
		
		Page<Roles> page = new Page<>();
		page.setCurrentPage(currentPage);
		page.setPageSize(20);
		params.put("page", page);
		
		List<Roles> roleList = roleService.findRolesListPage(params);
		page.setList(roleList);
		page.setRows((int)roleService.findRolesCountPage(params));
		
		results.put("page", page);
		return results;
	}
	
	@PostMapping("/insertRoles")
	@ResponseBody
	public Result insertRoles(Roles roles, HttpSession session) {
		try {
			Map<String, Object> params = new HashMap<>();
			Map<String, Object> results = new HashMap<>();
			Users user = (Users)session.getAttribute("current_user");
			String medicalMainstayId = user!=null?user.getMedicalMainstayId():null;
			roles.setMedicalMainstayId(medicalMainstayId);
			params.put("roleName", roles.getRoleName());
			params.put("medicalMainstayId", medicalMainstayId);
			
			if(roleService.findCountByRoleName(params) > 0) {
				return new Result(false, "该角色名称已经存在!");
			}
			
			if(roleService.insertRoles(roles) > 0) {
				return new Result(true, "添加成功!");
			}
			return new Result(false, "添加失败!");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		}
		
	}
	
	@GetMapping("/findRoleById")
	@ResponseBody
	public Map<String, Object> findRoleById(String id, HttpSession session) {
			Map<String, Object> params = new HashMap<>();
			Map<String, Object> results = new HashMap<>();
			Users user = (Users)session.getAttribute("current_user");
			String medicalMainstayId = user!=null?user.getMedicalMainstayId():null;
			params.put("medicalMainstayId", medicalMainstayId);
			params.put("id", id);
			Roles role = roleService.findRoleById(params);
			results.put("role", role);
			
			return results;
	}
	
	
	@PostMapping("/updateRoles")
	@ResponseBody
	public Result updateRoles(Roles roles, HttpSession session) {
		try {
			Map<String, Object> params = new HashMap<>();
			Map<String, Object> results = new HashMap<>();
			Users user = (Users)session.getAttribute("current_user");
			String medicalMainstayId = user!=null?user.getMedicalMainstayId():null;
			roles.setMedicalMainstayId(medicalMainstayId);
			params.put("roleName", roles.getRoleName());
			params.put("medicalMainstayId", medicalMainstayId);
			params.put("id", roles.getId());
			
			Roles oldRoles = roleService.findRoleById(params);
			if(!oldRoles.getRoleName().equals(roles.getRoleName()) && roleService.findCountByRoleName(params) > 0) {
				return new Result(false, "该角色名称已经存在!");
			}
		
			if(roleService.updateRoles(roles) > 0) {
				return new Result(true, "修改成功!");
			}
			
			return new Result(false, "修改失败!");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		}
		
	}
	
	@PostMapping("/deleteRoles")
	@ResponseBody
	public Result deleteRoles(String roleId, HttpSession session) {
		
		try {
			Map<String, Object> params = new HashMap<>();
			Map<String, Object> results = new HashMap<>();
			Users user = (Users)session.getAttribute("current_user");
			String medicalMainstayId = user!=null?user.getMedicalMainstayId():null;
			params.put("roleId", roleId);
			params.put("medicalMainstayId", medicalMainstayId);
			
			if(userRolesService.findCountByRoleId(params) > 0) {
				return new Result(false, "该角色下有用户,请先删除用户!");
			}
			return roleService.deleteRoleById(params);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		}
	}
	
	
	@PostMapping(value = "/updateRoleToAllAuthority")
	@ResponseBody
	public Result updateRoleToAllAuthority(String roleId, String authorityStr, HttpSession session) {
		try {
			Users user = (Users)session.getAttribute("current_user");
			String medicalMainstayId = user!=null?user.getMedicalMainstayId():null;
			
			List<RoleAuthoritys> roleAuthorityList = new ArrayList<>();
			if(StringUtils.isNotBlank(authorityStr)) {
				String[] authorityArray = authorityStr.split(",");
				for(String str : authorityArray) {
					RoleAuthoritys roleAuthority = new RoleAuthoritys();
					roleAuthority.setId(StringUtil.getId());
					roleAuthority.setRoleId(roleId);
					roleAuthority.setAuthorityId(str);
					roleAuthority.setMedicalMainstayId(medicalMainstayId);
					roleAuthorityList.add(roleAuthority);
				}
			}
			return roleAuthorityService.updateRoleToAllAuthority(roleId, medicalMainstayId, roleAuthorityList);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, e.getMessage());
		}
	}
	
	
	
}
